package com.liao.rent.utils;

import com.liao.rent.entity.SysUserDetails;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;

/**
 * @Author: ljt
 * @Description: wenb安全工具类 获取对象 对象权限等相关操作
 * @DateTime: 2024/12/9 下午10:16
 **/
public class WebSecurityUtils {

    public static SysUserDetails getUser(){
        return  (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 判断是不是管理员
     * @return 是 true 否 false
     */
    public static boolean isAdmin(){
        SysUserDetails principal = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = principal.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("admin")){
                return true;
            }
        }
        return false;
    }
    /**
     * 判断是不是房东
     * @return 是 true 否 false
     */
    public static boolean isLandlord(){
        SysUserDetails principal = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = principal.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("landlord")){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是不是房客
     * @return 是 true 否 false
     */
    public static boolean isRegularUsers(){
        SysUserDetails principal = (SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = principal.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (authority.getAuthority().equals("regular_users")){
                return true;
            }
        }
        return false;
    }
}
